dofile(os.getenv('LUAR_HOME') .. '/util/File.lua')

function execute()
	local file = File{}
	local http = require('socket.http')

	local component_id = arg[2]
	b = http.request('http://www.natalnet.br/~igorosberg/luar/components/' .. component_id .. '.tar.gz')

	if string.find( b, '<h1>Not Found</h1>') ~= nil then
		print( 'Unable to locate component ' .. component_id .. '. Check your connection or be ensure that component id is valid.')
		return false
	else
		if file:exists( os.getenv('LUAR_HOME') .. '/lib/components/' .. component_id ) then
			print( 'Already there is a component with name "' .. component_id .. '" added to luar. Overwrite component? (y/n)' )
			local res = io.read()
			while( res ~= 'y' and res ~= 'n' ) do
				print( 'Already there is a component with name "' .. component_id .. '" added to luar. Overwrite component? (y/n)' )
				res = io.read()
			end
			if res == 'n' then
				return false
			end
			assert( os.execute( 'rm -r' .. os.getenv('LUAR_HOME') .. '/lib/components/' .. component_id ) )
		end
		local f = assert( io.open( os.getenv('LUAR_HOME') .. '/tmp/' .. component_id .. '.tar.gz','w') );
		assert( f:write( b ) );
		assert( f:flush() );
		assert( f:close() );
		
		assert( os.execute( 'tar -zxf ' .. os.getenv('LUAR_HOME') .. '/tmp/' .. component_id .. '.tar.gz -C ' .. os.getenv('LUAR_HOME') .. '/tmp/' ) )
		assert( os.execute( 'cp -r ' .. os.getenv('LUAR_HOME') .. '/tmp/' .. component_id .. ' ' .. os.getenv('LUAR_HOME') .. '/lib/components/' ) )
		assert( os.execute( 'rm -r ' .. os.getenv('LUAR_HOME') .. '/tmp/' .. component_id ) )
		assert( os.execute( 'rm -r ' .. os.getenv('LUAR_HOME') .. '/tmp/' .. component_id .. '.tar.gz ' ) )
		
		return true		
	end
end
